﻿package cminus.ast;

import java.util.HashMap;
import java.util.Map;

import cminus.CminusException;


/**
 * C1语言AST结点的基类 
 */
public abstract class ASTNode implements cminus.AbstractSyntaxTree {
	
	/** 定义AST结点类型编号常量 */ 
	public static final int PROGRAM = 1;
	public static final int STATEMENT_LIST = 2;
	public static final int BLOCK = 3;
	public static final int ASSIGNMENT_STATEMENT = 4;
	public static final int EXPRESSION_STATEMENT = 5;
	public static final int IF_STATEMENT = 6;
	public static final int WHILE_STATEMENT = 7;
	public static final int BREAK_STATEMENT = 8;
	public static final int CONTINUE_STATEMENT = 9;
	public static final int INPUT_STATEMENT = 10;
	public static final int OUTPUT_STATEMENT = 11;
	public static final int POS_EXPRESSION = 12;
	public static final int NEG_EXPRESSION = 13;
	public static final int NOT_EXPRESSION = 14;
	public static final int ADD_EXPRESSION = 15;
	public static final int SUB_EXPRESSION = 16;
	public static final int MUL_EXPRESSION = 17;
	public static final int LESS_EXPRESSION = 18;
	public static final int LESS_EQUAL_EXPRESSION = 19;
	public static final int GREATER_EXPRESSION = 20;
	public static final int GREATER_EQUAL_EXPRESSION = 21;
	public static final int EQUAL_EXPRESSION = 22;
	public static final int NOT_EQUAL_EXPRESSION = 23;
	public static final int AND_EXPRESSION = 24;
	public static final int OR_EXPRESSION = 25;
	public static final int	IDENTIFIER = 26;	
	public static final int INTEGER_LITERAL = 27;
	
	/** 记录树节点的总个数 */
	public static  int sum = 0;
	
	/** 记录树节点的访问标志 */
	public int visited = 0;
	public int visitedjuli=0;
	public int visiteddraw=0;
	public String name="program";


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getVisiteddraw() {
		return visiteddraw;
	}

	public void setVisiteddraw(int visiteddraw) {
		this.visiteddraw = visiteddraw;
	}

	public int getVisitedjuli() {
		return visitedjuli;
	}

	public void setVisitedjuli(int visitedjuli) {
		this.visitedjuli = visitedjuli;
	}
	/** 记录树节点的总个数 */
	private  int layer;

	/** 存储该AST结点的父结点 */
	private ASTNode parent;
	
	/** 存储该AST结点的孩子结点个数 */
	private int childrenNum;
	
	/** 存储该AST结点的孩子结点 */
	private ASTNode childIP[]=new ASTNode[10];
	
	/** 存储该AST结点所在的行号 */
	private final int line;
	
	/** 存储该AST结点所在的列号 */
	private final int column;
	/** 存储该AST结点所在行的节点个数 */
	private  int layerNum;
	public int getLayerNum() {
		return layerNum;
	}

	public void setLayerNum(int layNum) {
		this.layerNum = layNum;
	}
	/** 存储该AST结点所在行之间的距离 */
	
	private double juli;
	/**定义坐标*/
	private double x;
	private double y;
	
	public double getX() {
		return x;
	}

	public void setX(double x) {
		this.x = x;
	}

	public double getY() {
		return y;
	}

	public void setY(double y) {
		this.y = y;
	}

	public double getJuli() {
		return juli;
	}

	public void setJuli(double juli) {
		this.juli = juli;
	}
	/** 存储该AST结点属性的Map  */
	private Map<String, Object> propertiesMap; 
	
	public static int getSum() {
		return sum;
	}

	public static void setSum(int sum) {
		ASTNode.sum = sum;
	}

	public  int getVisited() {
		return this.visited;
	}

	public void setVisited(int visited) {
		this.visited = visited;
	}

	public int getLayer() {
		return layer;
	}

	public void setLayer(int layer) {
		this.layer = layer;
	}

	public ASTNode getParent() {
		return parent;
	}
	
	public void setParent(ASTNode parent) {
		this.parent = parent;
	}
	
	public int getChildrenNum() {
		return childrenNum;
	}

	public void setChildrenNum(int childrenNum) {
		this.childrenNum = childrenNum;
	}

	
/**
 * 获取第一个儿子节点的指针
 * @return
 */
	public ASTNode getChildIP() {
		return childIP[0];
	}
	
	public ASTNode getChildIP(int i) {
		return childIP[i];
	}
	
	public void setChildIP(ASTNode s,int i){
		this.childIP[i]=s;
	}

	public void setChildIP(ASTNode[] childIP,int n) {
		this.childrenNum = n;
		for(int i=0;i<n;i++)
			this.childIP[i] = childIP[i];
	}

	public void setChildIP(ASTNode childIP) {
		this.childrenNum = 1;
		this.childIP[0] = childIP;
	}
	
	public int getLine() {
		return line;
	}

	public int getColumn() {
		return column;
	}
	
	public ASTNode(int line, int column) {
		this.line = line;
		this.column = column;
		propertiesMap = new HashMap<String, Object>();
		
	}
	
	/**
	 * 接受访问者访问该AST结点的方法
	 * @param visitor 访问者
	 */
	public abstract void accept(ASTVisitor visitor) throws CminusException;
	
	/**
	 * 获取AST结点的某属性
	 * @param name 属性名称
	 * @return 属性值
	 */
	public Object getProperty(String name) {
		return propertiesMap.get(name);
	}
	
	/**
	 * 设置AST结点的某属性
	 * @param name 属性名称
	 * @param value 属性值
	 */
	public void setProperty(String name, Object value) {
		propertiesMap.put(name, value);
	}
	
}
